$(document).ready(

  function()
  {
	$(".post-vote.up.question").click(
		function()					
		{
			var id = $(".post-vote.up.question").children("p").html()
			id = id.toString()
			var type = 'question vote up'
            $.post("/qa/vote/ajax/", {id:id,type:type},function(data)
            {
	            var message = data.split(' ')
	            if(message[0] == 'on')
	            {	 
		             $("#post-question-"+message[2]+"-score").text(message[1])
		             $(".post-vote.up.question").addClass("on")
	            }
	            else if(message[0] == 'off')
	            {
		             $("#post-question-"+message[2]+"-score").text(message[1])
		             $(".post-vote.up.question").removeClass(" on")	
	            }
	            else
	            {
		
	            }
			});			 
		}
	);	
	
	
	$(".post-vote.down.question").click(
		function()					
		{
			var id = $(".post-vote.up.question").children("p").html()
			id = id.toString()
			var type = 'question vote down'
            $.post("/qa/vote/ajax/", {id:id,type:type},function(data)
            {
	            var message = data.split(' ')
	            if(message[0] == 'on')
	            {	 
		             $("#post-question-"+message[2]+"-score").text(message[1])
		             $(".post-vote.down.question").addClass("on")
	            }
	            else if(message[0] == 'off')
	            {
		             $("#post-question-"+message[2]+"-score").text(message[1])
		             $(".post-vote.down.question").removeClass(" on")	
	            }
	            else
	            {
		
	            }
			});			 

		}
	);
	$("#favorite-mark").click(
		function()					
		{
			var id = $(".post-vote.up.question").children("p").html()
			id = id.toString()
			var type = 'mark favourite'
            $.post("/qa/vote/ajax/", {id:id,type:type},function(data)
            {
	            var message = data
	            if(message == 'on')
	            {	 
		             $("#favorite-mark").addClass("on")
	            }
	            else if(message == 'off')
	            {
		             $("#favorite-mark").removeClass(" on")	
	            }
	            else
	            {
		
	            }
			});			 
		}
	);	
	$(".post-vote.up.answer").click(
		function()					
		{
			var object = $(this)
			var id = object.attr('id')
			var message = id.split(' ')
			var type = 'answer vote up'
            $.post("/qa/vote/ajax/", {id:message[1],type:type},function(data)
            {
	            message = data.split(' ')
	            if(message[0] == 'on')
	            {	 
		             $("#post-answer-"+message[2]+"-score").text(message[1])
		             object.addClass("on")
	            }
	            else if(message[0] == 'off')
	            {    
		             $("#post-answer-"+message[2]+"-score").text(message[1])
		             object.removeClass("on")	
	            }
	            else
	            {
		
	            }
			});		

		}
	);	
	$(".post-vote.down.answer").click(
		function()					
		{
			var object = $(this)
			var id = object.attr('id')
			var message = id.split(' ')
			var type = 'answer vote down'
            $.post("/qa/vote/ajax/", {id:message[1],type:type},function(data)
            {
	            var message = data.split(' ')
	            if(message[0] == 'on')
	            {	 
		             $("#post-answer-"+message[2]+"-score").text(message[1])
		             object.addClass("on")
	            }
	            else if(message[0] == 'off')
	            {
		             $("#post-answer-"+message[2]+"-score").text(message[1])
		             object.removeClass("on")	
	            }
	            else
	            {
		
		
	            }
			});		
		}
	);	
	//toggle preview of editor

     var display = true;
     var txt = '[禁用预览]';
     $('#pre-collapse').text(txt);
     $('#pre-collapse').bind('click', function()
	 {
     txt = display ? "[启用预览]" : "[禁用预览]";
     display = !display;
     $('#wmd-preview').toggle();
     $('#pre-collapse').text(txt);
      });	
	
	  $('#editor_side_bar').toggle();
	  $('#tags_side_bar').toggle();

	  $('#id_tags').focus(function(){
		$('#title_side_bar').hide();
		$('#tags_side_bar').show();
		$('#editor_side_bar').hide();});
	  
	  $('#id_title').focus(function(){
		$('#title_side_bar').show();
		$('#tags_side_bar').hide();
		$('#editor_side_bar').hide();})
	  
	  
	 if ($('#wmd-input').length) 
	 {
         var $editor = $('#wmd-input');
        var $previewer = $('#wmd-preview');
        var $container = $('#editor-metrics');

        var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
        var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
        var editor_interval = null;

        $editor.focus(function() {
		$('#title_side_bar').hide();
		$('#tags_side_bar').hide();
		$('#editor_side_bar').show();
            if (editor_interval == null) {
                editor_interval = window.setInterval(function() {
                    recalc_metrics();
                }, 200);
            }
        });

        function recalc_metrics() {
			
          var text = $previewer.text();

            var char_count = text.length;
            var fullStr = text + " ";
            var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
            var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
            var splitString = cleanedStr.split(" ");
            var word_count = splitString.length - 1;

            var metrics = char_count + " " + (char_count == 1 ? messages.character : messages.characters);
            metrics += " / " + word_count + " " + (word_count == 1 ? 'word' : 'words');
            $container.html(metrics);
        }
    }
  }
 );